package com.seio.edc.controller.system;

import com.nova.edc.model.energy.OPerate;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.service.energy.OPerateService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.seio.edc.controller.core.BaseContoller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/operate")
public class OPerateContronller extends BaseContoller {

	private static final int PAGE_SIZE = 10;

	@Autowired
	private OPerateService novaOPerateService;
	@Autowired
	private CustomerInfoService novaCustomerInfoService;

	/**
	 * 公司运营管理列表
	 */
	@RequestMapping(value = "operatelist.shtml")
	public ModelAndView oPerateList(HttpServletRequest request,
			HttpSession session, Integer pageIndex, OPerate oPerate,
			Integer customerinfoid, ModelMap modelMap) {
		// 当前页数
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		modelMap.put("pageIndex", page.getCurrentPage());
		oPerate.setCustomerinfoid(customerinfoid);
		PageList<OPerate> pageList = novaOPerateService.findPageList(oPerate,
				page, "currentdate", "desc");
		CustomerInfo customerInfo = novaCustomerInfoService
				.getByIdused(customerinfoid);
		// 数据列表
		modelMap.put("customerInfo", customerInfo);
		modelMap.put("companycode", customerInfo.getCompanycode());
		modelMap.put("customerinfoid", customerInfo.getId());
		modelMap.put("customername", customerInfo.getCustomername());
		modelMap.put("list", pageList.getDatas());
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("pageCount", page.getTotalPage());
		return new ModelAndView("operate/operatelist", modelMap);
	}

	/**
	 * 添加公司运营数据
	 */
	@RequestMapping(value = "operateedit.shtml")
	public ModelAndView editOPerateInit(HttpServletRequest request,
			HttpSession session, ModelMap modelMap, Integer customerinfoid,
			Integer operateId) {
		CustomerInfo customerInfo = novaCustomerInfoService.getById(customerinfoid);
		modelMap.put("customerInfo", customerInfo);
		if (operateId != null && operateId != 0) {
			OPerate oPerate = novaOPerateService.getById(operateId);
			modelMap.put("oPerate", oPerate);
		}
		return new ModelAndView("operate/operateedit", modelMap);
	}

	/**
	 * 保存运营数据
	 */
	@RequestMapping(value = "saveoperate.shtml")
	public ModelAndView saveOperate(OPerate oPerate, BindingResult result,
			HttpServletRequest request, HttpSession session, ModelMap modelMap) {
		if (oPerate.getId() != null && oPerate.getId() != 0) {
			novaOPerateService.update(oPerate);
		} else {
			novaOPerateService.insert(oPerate);
		}
		return oPerateList(request, session, 1, oPerate,
				oPerate.getCustomerinfoid(), modelMap);
	}

	@RequestMapping(value = "deleteoperate.shtml")
	public ModelAndView deleteoperate(Integer id, HttpServletRequest request,
			HttpSession session, ModelMap modelMap) {
		OPerate oPerate = novaOPerateService.getById(id);
		novaOPerateService.deleteById(id);
		return oPerateList(request, session, 1, new OPerate(),
				oPerate.getCustomerinfoid(), modelMap);
	}

}
